import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login/LoginIndex.vue'
import Layout from '../views/Layout/LayouIndex.vue'
import store from '@/store'

// 异步处理
const Home =()=>import('../views/Home/HomeIndex.vue')
const Params=()=>import('../views/Params/ParamsIndex.vue')
const Goods=()=>import('../views/Goods/GoodsIndex.vue')
const MysmallmengIndex=()=>import('../views/Mysmallmeng/MysmallmengIndex.vue')
const Hope=()=>import('../views/Hope/HopeIndex.vue')
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    component: Layout,
    //路由元信息
    meta:{
      isLogin:true
    },
    children:[
      {
        path:'/home',
        component:Home
      },
      {
        path:'/params',
        component:Params
      },
      {
        path:'/goods',
        component:Goods
      },
      {
        path:'/mysmallmeng',
        component:MysmallmengIndex,
      },
      {
        path:'/hope',
        component:Hope,
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: Login,

  }

]

const router = new VueRouter({
  routes
})

router.beforeEach((to,from,next)=>{
  // console.log('-to-',to);
  //判断是否需要登录
  if(to.matched.some(record => record.meta.isLogin)){
    //2、判断当前用户是否已经登录
    // let token=''
    let token=store.state.loginModule.userinfo.token
    if(token){
      next();
      } 
      else{
        next('/login')
    }
  }else{//不需要登录
      next()
  }

  
})

export default router
